import Vue from 'vue';
import siteInfo from './siteInfo.js';

export default {
	isDD: function() {
		if (`${siteInfo.platform}` === 'mobile') {
			return 1;
		} else {
			return 0;
		}
	},

	getConfig({
		success,
		fail
	}) {
		dd.ready(function() {
			dd.getConfig().then(res => {
				console.log(res)
				success(res)
			}).catch(err => {
				console.log(err)
				fail(err)
			})
		})
	},
	init: function(jianquan = 0) {
		return new Promise((resolve, reject) => {
			dd.ready(function() {
				if (jianquan) {
					Vue.prototype.$request({
						url: Vue.prototype.$api.dd.config,
					}).then((e) => {
						if (e.code == 0) {
							let ticket = e.info.accessToken
							let jsApiList = [
								'getGeolocation',
							];
							let config = {
								ticket: ticket,
								jsApiList: jsApiList,
							};
							dd.authConfig(config).then(res => {
								resolve(dd);
							}).catch(err => {
								reject(err)
								console.log('config fail:', err);
							})
						}
					})
				} else {
					resolve(dd);
				}
			})
		})

	},
	getLocation: function({
		success,
		fail
	}) {
		if (this.isDD()) {
			this.init(1).then(jdd => {
				jdd.getGeolocation({
					targetAccuracy: 100,
					coordinate: 1,
					withReGeocode: false,
					useCache: true, //默认是true，如果需要频繁获取地理位置，请设置false
				}).then(res => {
					success(res);
					console.log(res)
				}).catch(err => {
					fail(err);
				})
			}).catch(err => {
				fail(err)
			});
		}
	},
	
	
	
	getAuthCode: function({
		success,
		fail
	}) {
		if (this.isDD()) {
			this.init().then(jdd => {
				jdd.getAuthCode({
				    corpId: ""
				}).then(res => {
					console.log('getAuthCode res', res)
					success(res);
				}).catch(err => {
					console.log('getAuthCode err', err)
				})
				
			}).catch(err => {
				fail(err)
			});
		}
	},
	

	takePhoto: function({
		success,
		fail
	}) {
		if (this.isDD()) {
			this.init().then(jdd => {
				jdd.takePhoto().then(res => {
					console.log('takePhoto', res)
					success(res);
				}).catch(err => {
					console.log('takePhoto', err)
					fail(err);
				})
			}).catch(err => {
				fail(err)
			});
		}
	},

	reduceImageSize: function({
		file,
		success,
		fail
	}) {
		if (this.isDD()) {
			this.init().then(jdd => {
				jdd.reduceImageSize({
					filePaths: file,
					compressLevel: 1,
					// compressNum:50
				}).then(res => {
					console.log('reduceImageSize', res)
					success(res);
				}).catch(err => {
					console.log('reduceImageSize', err)
					fail(err);
				})

			}).catch(err => {
				fail(err)
			});
		}
	},

	startRecordAudio: function({
		success,
		fail
	}) {
		if (this.isDD()) {
			this.init().then(jdd => {
				jdd.startRecordAudio().then(res => {
					console.log('startRecordAudio', res)
					success(res);
				}).catch(err => {
					console.log('startRecordAudio', err)
					fail(err);
				})
			}).catch(err => {
				fail(err)
			});
		}
	},
	stopRecordAudio: function({
		success,
		fail
	}) {
		if (this.isDD()) {
			this.init().then(jdd => {
				jdd.stopRecordAudio().then(res => {
					console.log('stopRecordAudio', res)
					success(res);
				}).catch(err => {
					console.log('stopRecordAudio', err)
					fail(err);
				})
			}).catch(err => {
				fail(err)
			});
		}
	},
	downloadAudio: function({
		mediaId,
		success,
		fail
	}) {
		if (this.isDD()) {
			this.init().then(jdd => {
				jdd.downloadAudio({
					mediaId: mediaId
				}).then(res => {
					console.log('downloadAudio', res)
					success(res);
				}).catch(err => {
					console.log('downloadAudio', err)
					fail(err);
				})
			}).catch(err => {
				fail(err)
			});
		}
	},


	uploadFile: function({
		file,
		success,
		fail
	}) {
		if (this.isDD()) {
			this.init().then(jdd => {
				jdd.uploadFile({
					filePath: file //需要上传的本地文件路径
				}).then(res => {
					console.log('jdd=>uploadFile', res)
					success(res);
				}).catch(err => {
					console.log('jdd=>uploadFile', err)
					fail(err);
				})
			}).catch(err => {
				fail(err)
			});
		}
	},

	scanCode: function({
		config = 'all',
		success,
		fail
	}) {
		if (this.isDD()) {
			this.init().then(jdd => {
				jdd.scan({
					type: config,
				}).then(res => {
					console.log('scanCode', res)
					success(res);
				}).catch(err => {
					console.log('scanCode', err)
					fail(err);
				})
			}).catch(err => {
				fail(err)
			});
		}
	},
	ut: function({
		key,
		value, // JSON字符串 "{"corpId":"","type":""}" (由isv自由提供的点击类型，默认可以传空)
		success,
		fail
	}) {
		if (this.isDD()) {
			this.init().then(jdd => {
				jdd.ut({
					key: key,
					value: value
				}).then(res => {
					console.log('uploadFile', res)
					success(res);
				}).catch(err => {
					console.log('uploadFile', err)
					fail(err);
				})
			}).catch(err => {
				fail(err)
			});
		}
	},



	callPhone: function({
		success,
		fail,
		phone
	}) {
		if (this.isDD()) {
			this.init().then(jdd => {
				jdd.callPhone({
					phoneNum: phone, //电话号码
				}).then(res => {
					success(res);
				}).catch(err => {
					fail(err);
				})
			}).catch(err => {
				fail(err)
			});
		}
	},


	config: function() {
		if (this.isDD()) {
			this.init();
		}
	}
}